/**
 * @param {string} s
 * @return {string}
 */
 var replaceSpace = function(s) {
  let count = 0
  for(let i of s){
    if(i === ' '){
      count++
    }
  }
  let m = s.length - 1
  for(let i = 0;i< 2*count;i++){
    s += '0'
  }
  let str = s.split('')
  let n = str.length - 1
  for(let i = m;i >= 0;i--){
    if(str[i] === ' '){
      str[n--] = '0'
      str[n--] = '2'
      str[n--] = '%'
    }else{
      str[n--] = str[i]
    }
  }
  return str.join('')
};
console.log(replaceSpace('niha o'));